//
//  AFNetWorkingGet.m
//  个人项目-汽车
//
//  Created by 卢佳威 on 15/8/19.
//  Copyright (c) 2015年 卢佳威. All rights reserved.
//

#import "AFNetWorkingGet.h"

@implementation AFNetWorkingGet

+ (void)GETWithAFNByURL:(NSString *)urlStr completion:(void(^)(id result))block
{
    //判断本地文件
    //    if ([LocalSave judgeFileExists:<#(NSString *)#>]) {
    //        <#statements#>
    //    }
    //转码
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding :NSUTF8StringEncoding ];
    //创建请求管理类
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    //设置响应解析对象
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    //支持类型
    [manage.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain", nil]];
    //AFNGET请求
    [manage GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            block(result);
        }else
            NSLog(@"data is empty");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"request fail");
    }];
}

//判断网络连接
+ (BOOL)IntertReachStatus
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    
    return manager.reachable;
}

//网络无连接弹出提示框
+ (void)popAlertIfNoInter:(UIViewController *)VC
{
    if (![AFNetWorkingGet IntertReachStatus]) {
        
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前网络无连接" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [alert dismissViewControllerAnimated:YES completion:^{
                
            }];
            
            [VC.navigationController popViewControllerAnimated:YES];
            
        }];
        [alert addAction:action];
        [VC.navigationController presentViewController:alert animated:YES completion:^{
            
        }];
    }

}
@end
